package com.cotte.flow.hbase.dao;

import java.io.IOException;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cotte.flow.hbase.record.BrushCardRecord;
import com.cotte.flow.hbase.table.BrushCardStruct;

@Component
public class BrushCardDaoImpl {
	
	@Autowired
	private Connection connection;
	/**
	 * 持久化刷卡表
	 * @param brushCard
	 */
	public void putRecord(BrushCardRecord brushCard) {
		Table table = null;
		try{
			table = connection.getTable(TableName.valueOf(BrushCardStruct.TABLE));
			Put put = new Put((brushCard.getOdid_stepcode()).getBytes());
			put.addColumn(BrushCardStruct.FAMILY.getBytes(), BrushCardStruct.COLUMN_EMPCODE.getBytes(),brushCard.getEmpcode().getBytes());
			put.addColumn(BrushCardStruct.FAMILY.getBytes(), BrushCardStruct.COLUMN_STEPCODE.getBytes(), brushCard.getStepcode().getBytes());
			put.addColumn(BrushCardStruct.FAMILY.getBytes(), BrushCardStruct.COLUMN_SYSCODE.getBytes(), brushCard.getSysCode().getBytes());
			put.addColumn(BrushCardStruct.FAMILY.getBytes(), BrushCardStruct.COLUMN_ORDERCODE.getBytes(), brushCard.getOrderCode().getBytes());
			put.addColumn(BrushCardStruct.FAMILY.getBytes(), BrushCardStruct.COLUMN_LOADDATE.getBytes(), brushCard.getLoadDate().getBytes());
			table.put(put);
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				table.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 更新工时
	 * <p>Title: updateWorkHour</p>  
	 * <p>Description: </p>  
	 * @param odid_stepcode
	 * @param workHour
	 */
	public void updateWorkHour(String odid_stepcode,String workHour) {
		Table table = null;
		try {
			table = connection.getTable(TableName.valueOf(BrushCardStruct.TABLE));
			Put put = new Put(odid_stepcode.getBytes());
			put.addColumn(BrushCardStruct.FAMILY.getBytes(), 
					BrushCardStruct.COLUMN_WORKHOUR.getBytes(), 
					workHour.getBytes());
			table.put(put);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				table.close();
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取刷卡时间和工时
	 * <p>Title: getLoadDate</p>  
	 * <p>Description: </p>  
	 * @param rowkey
	 * @return
	 */
	public String getLoadDate(String rowkey){
		Table table = null;
		try {
			table = connection.getTable(TableName.valueOf(BrushCardStruct.TABLE));
			Get get = new Get(rowkey.getBytes());
			Result result = table.get(get);
			byte[] loadDate = result.getValue(BrushCardStruct.FAMILY.getBytes(),BrushCardStruct.COLUMN_LOADDATE.getBytes());
			byte[] workHour = result.getValue(BrushCardStruct.FAMILY.getBytes(),BrushCardStruct.COLUMN_WORKHOUR.getBytes());
			if(loadDate != null && workHour != null){
				return new String(loadDate)+"_"+new String(workHour);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				table.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}